package com.micro.school.order.service.impl;

import com.micro.school.common.core.context.SecurityContextHolder;
import com.micro.school.common.core.domain.R;
import com.micro.school.order.entity.*;
import com.micro.school.order.mapper.PayPrepaymentImportMapper;
import com.micro.school.order.mapper.PayPrepaymentImportRecordMapper;
import com.micro.school.order.mapper.PayStudentPrepaymentMapper;
import com.micro.school.order.service.PayPrepaymentImportService;
import com.micro.school.order.service.PayPrepaymentProjectService;
import com.micro.school.order.util.OrderNoGenerator;
import com.micro.school.system.api.RemoteEduService;
import com.micro.school.system.api.RemoteUserService;
import com.micro.school.system.api.domain.StudentInfos;
import com.micro.school.system.api.domain.vo.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service("PayPrepaymentImportServiceImpl")
public class PayPrepaymentImportServiceImpl implements PayPrepaymentImportService {

    @Resource
    private PayPrepaymentImportMapper payPrepaymentImportMapper;

    @Resource
    private PayPrepaymentImportRecordMapper payPrepaymentImportRecordMapper;

    @Resource
    private PayStudentPrepaymentMapper payStudentPrepaymentMapper;

    @Resource
    private RemoteEduService remoteEduService;

    @Resource
    private PayPrepaymentProjectService payPrepaymentProjectService;

    @Resource
    private RemoteUserService remoteUserService;



    @Override
    public List<PayPrepaymentImport> selectStudentBatchLogList(PayPrepaymentImport payPrepaymentImport) {
        return payPrepaymentImportMapper.queryAllByLimit(payPrepaymentImport);
    }

    @Override
    public void insertStudentBatchLog(PayPrepaymentImport payPrepaymentImport, String status) {
        if (status != null && !"".equals(status)){
            payPrepaymentImport.setImportStatus(20);
        }else{
            payPrepaymentImport.setImportStatus(10);
        }
        payPrepaymentImportMapper.insert(payPrepaymentImport);
    }

    @Override
    public String repeatUpload(String batchId) {
        PayPrepaymentImportRecord payPrepaymentImportRecord = new PayPrepaymentImportRecord();
        payPrepaymentImportRecord.setBatchNum(batchId);
        payPrepaymentImportRecord.setImportStatus(20);
        List<PayPrepaymentImportRecord> payPrepaymentImportRecordList = payPrepaymentImportRecordMapper.queryAllByLimit(payPrepaymentImportRecord);
        if (payPrepaymentImportRecordList != null && payPrepaymentImportRecordList.size() > 0){
            return "有错误数据，重新导入失败！";
        }else{
            PayPrepaymentImportRecord payPrepaymentImportRecord1 = new PayPrepaymentImportRecord();
            payPrepaymentImportRecord1.setBatchNum(batchId);
            payPrepaymentImportRecord1.setImportStatus(10);
            List<PayPrepaymentImportRecord> payPrepaymentImportRecordList1 = payPrepaymentImportRecordMapper.queryAllByLimit(payPrepaymentImportRecord1);
            for (PayPrepaymentImportRecord record : payPrepaymentImportRecordList1) {
                PayStudentPrepayment payStudentPrepayment = new PayStudentPrepayment();
                payStudentPrepayment.setBillNo(OrderNoGenerator.generateOrderNo());
                // 基本信息
                payStudentPrepayment.setStudentName(record.getStudentName());
                payStudentPrepayment.setStudentId(record.getStudentId());
                payStudentPrepayment.setIdNumber(record.getIdNumber());

                // 查询学院信息
                SchoolCollegeVos schoolCollegeVos = new SchoolCollegeVos();
                schoolCollegeVos.setCollegeName(record.getCollegeName());
                schoolCollegeVos.setDeleteFlag("0");
                R<List<SchoolCollegeVos>> collegeResult = remoteEduService.checkCollege(schoolCollegeVos);

                if (collegeResult.getData() != null && !collegeResult.getData().isEmpty()) {
                    payStudentPrepayment.setCollegeId(collegeResult.getData().get(0).getId());
                    payStudentPrepayment.setCampusId(collegeResult.getData().get(0).getCampusId());
                }

                // 查询学年信息
                SchoolFeeYearVos schoolFeeYearVos = new SchoolFeeYearVos();
                schoolFeeYearVos.setDeleteFlag("0");
                schoolFeeYearVos.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
                schoolFeeYearVos.setStatus("1");
                schoolFeeYearVos.setIsCloseChannel("1");
                schoolFeeYearVos.setFeeYear(record.getFeeYear());
                R<List<SchoolFeeYearVos>> feeYearResult = remoteEduService.checkFeeYear(schoolFeeYearVos);
                if (feeYearResult.getData() != null && !feeYearResult.getData().isEmpty()) {
                    payStudentPrepayment.setFeeYearId(Math.toIntExact(feeYearResult.getData().get(0).getId()));
                    payStudentPrepayment.setFeeYear(feeYearResult.getData().get(0).getFeeYear());
                }

                // 查询学生信息
                StudentInfoVos studentInfoVos = new StudentInfoVos();
                studentInfoVos.setName(record.getStudentName());
                studentInfoVos.setIdNumber(record.getIdNumber());
                studentInfoVos.setIsDel(0);
                R<StudentInfos> studentResult = remoteUserService.queryStudent(studentInfoVos);

                if (studentResult != null && studentResult.getData() != null) {
                    payStudentPrepayment.setStudentId(Math.toIntExact(studentResult.getData().getId()));
                }

                // 查询模式信息
                CollegeModelVos collegeModelVos = new CollegeModelVos();
                collegeModelVos.setModelName(record.getModelName());
                collegeModelVos.setCollegeName(record.getCollegeName());
                if (collegeResult.getData() != null && !collegeResult.getData().isEmpty()) {
                    collegeModelVos.setCampusId(collegeResult.getData().get(0).getCampusId());
                }
                collegeModelVos.setDeleteFlag("0");
                collegeModelVos.setStatus("1");
                R<List<CollegeModelVos>> modelResult = remoteEduService.checkEducation(collegeModelVos);

                if (modelResult.getData() != null && !modelResult.getData().isEmpty()) {
                    payStudentPrepayment.setModelId(modelResult.getData().get(0).getId());
                }

                // 查询专业信息
                CollegeMajorVos collegeMajorVos = new CollegeMajorVos();
                if (collegeResult.getData() != null && !collegeResult.getData().isEmpty()) {
                    collegeMajorVos.setCollegeIds(String.valueOf(collegeResult.getData().get(0).getId()));
                }
                collegeMajorVos.setMajorName(record.getMajorName());
                collegeMajorVos.setDeleteFlag("0");
                collegeMajorVos.setStatus("1");
                R<List<CollegeMajorVos>> majorResult = remoteEduService.checkMajor(collegeMajorVos);

                if (majorResult.getData() != null && !majorResult.getData().isEmpty()) {
                    payStudentPrepayment.setMajorId(majorResult.getData().get(0).getId());
                }

                // 查询年级信息
                SchoolGradeVos schoolGradeVos = new SchoolGradeVos();
                schoolGradeVos.setGrade(record.getGradeName());
                schoolGradeVos.setDeleteFlag("0");
                schoolGradeVos.setStatus("1");
                R<List<SchoolGradeVos>> gradeResult = remoteEduService.checkGrade(schoolGradeVos);

                if (gradeResult.getData() != null && !gradeResult.getData().isEmpty()) {
                    payStudentPrepayment.setGradeId(gradeResult.getData().get(0).getId());
                }

                // 查询预缴费项目
                PayPrepaymentProject payPrepaymentProject = new PayPrepaymentProject();
                payPrepaymentProject.setProjectName(record.getPrepaymentSubject());
                payPrepaymentProject.setDeleteFlag("0");
                List<PayPrepaymentProject> projectList = payPrepaymentProjectService.queryList(payPrepaymentProject);
                if (projectList != null && !projectList.isEmpty()) {
                    payStudentPrepayment.setSubjectId(projectList.get(0).getId());
                }



                payStudentPrepayment.setCollegeName(record.getCollegeName());
                // 模式信息
                payStudentPrepayment.setModelName(record.getModelName());
                // 专业信息
                payStudentPrepayment.setMajorName(record.getMajorName());
                // 年级信息
                payStudentPrepayment.setGradeName(record.getGradeName());
                // 学年信息
                payStudentPrepayment.setFeeYear(record.getFeeYear());
                // 预缴费项目信息
                payStudentPrepayment.setProjectName(record.getPrepaymentSubject());
                // 金额信息
                payStudentPrepayment.setChargeAmount(record.getChargeAmount());
                // 初始抵扣金额为0
                payStudentPrepayment.setDeductionAmount(BigDecimal.valueOf(0));
                // 支付方式 (默认现金)
                payStudentPrepayment.setPrepaymentMethod(record.getPrepaymentMethod());
                // 租户信息
                // 状态信息
                // 初始支付状态为未支付
                payStudentPrepayment.setPaymentStatus(10);
                // 初始归集状态为未归集
                payStudentPrepayment.setCollectionStatus(10);
                payStudentPrepayment.setCreateTime(new Date());
                payStudentPrepayment.setUpdateTime(new Date());
                payStudentPrepayment.setCreateBy(SecurityContextHolder.getUserName());
                payStudentPrepayment.setUpdateBy(SecurityContextHolder.getUserName());
                payStudentPrepayment.setTenantId(Integer.valueOf(SecurityContextHolder.getUserTenantId()));
                payStudentPrepayment.setDeleteFlag("0");
                payStudentPrepayment.setIsCancel(0);
                // 保存预缴费记录
                payStudentPrepaymentMapper.insert(payStudentPrepayment);
            }
            payPrepaymentImportMapper.updateByBatchNum(batchId);
        }
        return "";
    }
}
